home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / arcers / tarsrc.zip / TARSRC.TAR / tar-1.11.2 / update.c < prev    next >
C/C++ Source or Header  |  1993-11-16  |  16KB  |  643 lines

  1. /* Update a tar archive.
  2.    Copyright (C) 1988, 1992 Free Software Foundation
  3.  
  4. This file is part of GNU Tar.
  5.  
  6. GNU Tar is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU Tar is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Tar; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* JF implement the 'r' 'u' and 'A' options for tar. */
  21. /* The 'A' option is my own invention:  It means that the file-names are
  22.    tar files, and they should simply be appended to the end of the archive.
  23.    No attempt is made to block the reads from the args; if they're on raw
  24.    tape or something like that, it'll probably lose. . . */
  25.  
  26. #include <sys/types.h>
  27. #include <stdio.h>
  28. #include <errno.h>
  29. #ifndef STDC_HEADERS
  30. extern int errno;
  31. #endif
  32.  
  33. #ifdef HAVE_SYS_MTIO_H
  34. #include <sys/ioctl.h>
  35. #include <sys/mtio.h>
  36. #endif
  37.  
  38. #ifdef BSD42
  39. #include <sys/file.h>
  40. #else
  41. #ifndef V7
  42. #include <fcntl.h>
  43. #endif
  44. #endif
  45.  
  46. #if !defined(__MSDOS__) && !defined(WINDOWSNT)
  47. #include <pwd.h>
  48. #include <grp.h>
  49. #endif
  50.  
  51. #define STDIN 0
  52. #define STDOUT 1
  53.  
  54. #include "tar.h"
  55. #include "port.h"
  56. #include "rmt.h"
  57.  
  58. int time_to_start_writing = 0;    /* We've hit the end of the old stuff,
  59.                    and its time to start writing new stuff
  60.                    to the tape.  This involves seeking
  61.                    back one block and re-writing the current
  62.                    block (which has been changed). */
  63.  
  64. char *output_start;        /* Pointer to where we started to write in
  65.                    the first block we write out.  This is used
  66.                    if we can't backspace the output and have
  67.                    to null out the first part of the block */
  68.  
  69. extern union record *head;
  70. extern struct stat hstat;
  71. extern long baserec;
  72.  
  73. #ifndef USE_PROTOTYPES
  74. extern void skip_file ();
  75. extern void skip_extended_headers ();
  76.  
  77. void append_file ();
  78. void close_archive ();
  79. int confirm ();
  80. void decode_header ();
  81. void fl_read ();
  82. void fl_write ();
  83. void flush_archive ();
  84. int move_arch ();
  85. struct name *name_scan ();
  86. char *name_from_list ();
  87. void name_expand ();
  88. void name_gather ();
  89. void names_notfound ();
  90. void open_archive ();
  91. int read_header ();
  92. void reset_eof ();
  93. void write_block ();
  94. void write_eot ();
  95. #else
  96. #include "update_p.h"
  97. #include "tar_p.h"
  98. #include "port_p.h"
  99. #include "buffer_p.h"
  100. #include "list_p.h"
  101. #include "create_p.h"
  102. #endif
  103.  
  104. /* Implement the 'r' (add files to end of archive), and 'u' (add files to
  105.    end of archive if they arent there, or are more up to date than the
  106.    version in the archive.) commands.*/
  107. void
  108. update_archive ()
  109. {
  110.   int found_end = 0;
  111.   int status = 3;
  112.   int prev_status;
  113.   char *p;
  114.   struct name *name;
  115. #ifndef USE_PROTOTYPES
  116.   extern void dump_file ();
  117. #endif
  118.  
  119.   name_gather ();
  120.   if (cmd_mode == CMD_UPDATE)
  121.     name_expand ();
  122.   open_archive (2);        /* Open for updating */
  123.  
  124.   do
  125.     {
  126.       prev_status = status;
  127.       status = read_header ();
  128.       switch (status)
  129.     {
  130.     case EOF:
  131.       found_end = 1;
  132.       break;
  133.  
  134.     case 0:        /* A bad record */
  135.       userec (head);
  136.       switch (prev_status)
  137.         {
  138.         case 3:
  139.           msg ("This doesn't look like a tar archive.");
  140.           /* FALL THROUGH */
  141.         case 2:
  142.         case 1:
  143.           msg ("Skipping to next header");
  144.         case 0:
  145.           break;
  146.         }
  147.       break;
  148.  
  149.       /* A good record */
  150.     case 1:
  151.       /* printf("File %s\n",head->header.name); */
  152.       /* head->header.name[NAMSIZ-1]='\0'; */
  153.       if (cmd_mode == CMD_UPDATE && (name = name_scan (current_file_name)))
  154.         {
  155.  
  156.           /* struct stat hstat; */
  157.           struct stat nstat;
  158.           int head_standard;
  159.  
  160.           decode_header (head, &hstat, &head_standard, 0);
  161.           if (stat (current_file_name, &nstat) < 0)
  162.         {
  163.           msg_perror ("can't stat %s:", current_file_name);
  164.         }
  165.           else
  166.         {
  167.           if (hstat.st_mtime >= nstat.st_mtime)
  168.             name->found++;
  169.         }
  170.         }
  171.       userec (head);
  172.       if (head->header.isextended)
  173.         skip_extended_headers ();
  174.       skip_file ((long) hstat.st_size);
  175.       break;
  176.  
  177.     case 2:
  178.       ar_record = head;
  179.       found_end = 1;
  180.       break;
  181.     }
  182.     }
  183.   while (!found_end);
  184.  
  185.   reset_eof ();
  186.   time_to_start_writing = 1;
  187.   output_start = ar_record->charptr;
  188.  
  189.   while (p = name_from_list ())
  190.     {
  191.       if (f_confirm && !confirm ("add", p))
  192.     continue;
  193.       if (cmd_mode == CMD_CAT)
  194.     append_file (p);
  195.       else
  196.     dump_file (p, -1, 1);
  197.     }
  198.  
  199.   write_eot ();
  200.   close_archive ();
  201.   names_notfound ();
  202. }
  203.  
  204. /* Catenate file p to the archive without creating a header for it.  It had
  205.    better be a tar file or the archive is screwed */
  206.  
  207. void
  208. append_file (p)
  209.      char *p;
  210. {
  211.   int fd;
  212.   struct stat statbuf;
  213.   long bytes_left;
  214.   union record *start;
  215.   long bufsiz, count;
  216.  
  217.   if (0 != stat (p, &statbuf) || (fd = open (p, O_RDONLY | O_BINARY)) < 0)
  218.     {
  219.       msg_perror ("can't open file %s", p);
  220.       errors++;
  221.       return;
  222.     }
  223.  
  224.   bytes_left = statbuf.st_size;
  225.  
  226.   while (bytes_left > 0)
  227.     {
  228.       start = findrec ();
  229.       bufsiz = endofrecs ()->charptr - start->charptr;
  230.       if (bytes_left < bufsiz)
  231.     {
  232.       bufsiz = bytes_left;
  233.       count = bufsiz % RECORDSIZE;
  234.       if (count)
  235.         bzero (start->charptr + bytes_left, (int) (RECORDSIZE - count));
  236.     }
  237.       count = read (fd, start->charptr, bufsiz);
  238.       if (count < 0)
  239.     {
  240.       msg_perror ("read error at byte %ld reading %d bytes in file %s", statbuf.st_size - bytes_left, bufsiz, p);
  241.       exit (EX_ARGSBAD);    /* FOO */
  242.     }
  243.       bytes_left -= count;
  244.       userec (start + (count - 1) / RECORDSIZE);
  245.       if (count != bufsiz)
  246.     {
  247.       msg ("%s: file shrunk by %d bytes, yark!", p, bytes_left);
  248.       abort ();
  249.     }
  250.     }
  251.   (void) close (fd);
  252. }
  253.  
  254. #ifdef DONTDEF
  255. bprint (fp, buf, num)
  256.      FILE *fp;
  257.      char *buf;
  258. {
  259.   int c;
  260.  
  261.   if (num == 0 || num == -1)
  262.     return;
  263.   fputs (" '", fp);
  264.   while (num--)
  265.     {
  266.       c = *buf++;
  267.       if (c == '\\')
  268.     fputs ("\\\\", fp);
  269.       else if (c >= ' ' && c <= '~')
  270.     putc (c, fp);
  271.       else
  272.     switch (c)
  273.       {
  274.       case '\n':
  275.         fputs ("\\n", fp);
  276.         break;
  277.       case '\r':
  278.         fputs ("\\r", fp);
  279.         break;
  280.       case '\b':
  281.         fputs ("\\b", fp);
  282.         break;
  283.       case '\0':
  284.         /* fputs("\\-",fp); */
  285.         break;
  286.       default:
  287.         fprintf (fp, "\\%03o", c);
  288.         break;
  289.       }
  290.     }
  291.   fputs ("'\n", fp);
  292. }
  293.  
  294. #endif
  295.  
  296. int number_of_blocks_read = 0;
  297.  
  298. int number_of_new_records = 0;
  299. int number_of_records_needed = 0;
  300.  
  301. union record *new_block = 0;
  302. union record *save_block = 0;
  303.  
  304. void
  305. junk_archive ()
  306. {
  307.   int found_stuff = 0;
  308.   int status = 3;
  309.   int prev_status;
  310.   struct name *name;
  311.  
  312.   /* int dummy_head; */
  313.   int number_of_records_to_skip = 0;
  314.   int number_of_records_to_keep = 0;
  315.   int number_of_kept_records_in_block;
  316.   int sub_status;
  317.   extern int write_archive_to_stdout;
  318.   int start_baserec;
  319.  
  320. #ifdef JUNK_DEBUG
  321.   fprintf(stderr,"Junk files\n");
  322. #endif
  323.   name_gather ();
  324.   open_archive (2);
  325.  
  326.   while (!found_stuff)
  327.     {
  328.       prev_status = status;
  329.       status = read_header ();
  330. #ifdef JUNK_DEBUG
  331.       printf("Header status %d\n", status);
  332. #endif
  333.       switch (status)
  334.     {
  335.     case EOF:
  336.       found_stuff = 1;
  337.       break;
  338.  
  339.     case 0:
  340.       userec (head);
  341.       switch (prev_status)
  342.         {
  343.         case 3:
  344.           msg ("This doesn't look like a tar archive.");
  345.           /* FALL THROUGH */
  346.         case 2:
  347.         case 1:
  348.           msg ("Skipping to next header");
  349.           /* FALL THROUGH */
  350.         case 0:
  351.           break;
  352.         }
  353.       break;
  354.  
  355.     case 1:
  356. #ifdef JUNK_DEBUG
  357.       head->header.arch_name[NAMSIZ-1] = '\0';
  358.       fprintf(stderr, "file %s, size %ld\n", head->header.arch_name,
  359.                   hstat.st_size);
  360. #endif
  361.       if ((name = name_scan (current_file_name)) == (struct name *) 0)
  362.         {
  363.           userec (head);
  364.           /* fprintf(stderr,"Skip %ld\n",(long)(hstat.st_size)); */
  365.           if (head->header.isextended)
  366.         skip_extended_headers ();
  367.           skip_file ((long) (hstat.st_size));
  368.           break;
  369.         }
  370.       name->found = 1;
  371.       found_stuff = 2;
  372.       break;
  373.  
  374.     case 2:
  375.       found_stuff = 1;
  376.       break;
  377.     }
  378.     }
  379.   /* fprintf(stderr,"Out of first loop\n"); */
  380.  
  381.   if (found_stuff != 2)
  382.     {
  383.       write_eot ();
  384.       close_archive ();
  385.       names_notfound ();
  386.       return;
  387.     }
  388.  
  389.   if (write_archive_to_stdout)
  390.     write_archive_to_stdout = 0;
  391.   new_block = (union record *) malloc (blocksize);
  392.   if (new_block == 0)
  393.     {
  394.       msg ("Can't allocate secondary block of %d bytes", blocksize);
  395.       exit (EX_SYSTEM);
  396.     }
  397.  
  398.   /* Save away records before this one in this block */
  399.   number_of_new_records = ar_record - ar_block;
  400.   number_of_records_needed = blocking - number_of_new_records;
  401.   if (number_of_new_records)
  402.     bcopy ((void *) ar_block, (void *) new_block, (number_of_new_records) * RECORDSIZE);
  403.  
  404. #ifdef JUNK_DEBUG
  405.   fprintf(stderr,"Saved %d recs, need %d more\n",number_of_new_records,number_of_records_needed);
  406. #endif
  407.  
  408.   start_baserec = baserec;
  409. #ifdef JUNK_DEBUG
  410.   printf("baserec is %d\n", baserec);
  411. #endif
  412.   
  413.   userec (head);
  414.   if (head->header.isextended)
  415.     skip_extended_headers ();
  416.   skip_file ((long) (hstat.st_size));
  417.   found_stuff = 0;
  418.   /* goto flush_file; */
  419.  
  420. #ifdef JUNK_DEBUG
  421.   printf("baserec is %d\n", baserec);
  422. #endif
  423.   number_of_blocks_read = (baserec-start_baserec)/blocking;
  424. #ifdef JUNK_DEBUG
  425.   printf("num_read %d\n", number_of_blocks_read);
  426. #endif
  427.   
  428.   for (;;)
  429.     {
  430.       /* Fill in a block */
  431.       /* another_file: */
  432.       if (ar_record == ar_last)
  433.     {
  434. #ifdef JUNK_DEBUG
  435.           fprintf(stderr,"New block\n");
  436. #endif
  437.       flush_archive ();
  438.       number_of_blocks_read++;
  439.     }
  440.       sub_status = read_header ();
  441. #ifdef JUNK_DEBUG
  442.       fprintf(stderr,"Header type %d\n",sub_status);
  443. #endif
  444.  
  445.       if (sub_status == 2 && f_ignorez)
  446.     {
  447.       userec (head);
  448.       continue;
  449.     }
  450.       if (sub_status == EOF || sub_status == 2)
  451.     {
  452.       found_stuff = 1;
  453.       bzero (new_block[number_of_new_records].charptr, RECORDSIZE * number_of_records_needed);
  454.       number_of_new_records += number_of_records_needed;
  455.       number_of_records_needed = 0;
  456.       write_block (0);
  457.       break;
  458.     }
  459.  
  460.       if (sub_status == 0)
  461.     {
  462.       msg ("Deleting non-header from archive.");
  463.       userec (head);
  464.       continue;
  465.     }
  466.  
  467.       /* Found another header.  Yipee! */
  468. #ifdef JUNK_DEBUG
  469.       head->header.arch_name[NAMSIZ-1] = '\0';
  470.       fprintf(stderr, "File %s, size %ld\n", head->header.arch_name,
  471.               hstat.st_size);
  472. #endif
  473.       if (name = name_scan (current_file_name))
  474.     {
  475.       name->found = 1;
  476.       /* fprintf(stderr,"Flush it\n"); */
  477.       /* flush_file: */
  478.       /* decode_header(head,&hstat,&dummy_head,0); */
  479.       userec (head);
  480.       number_of_records_to_skip = (hstat.st_size + RECORDSIZE - 1) / RECORDSIZE;
  481.       /* fprintf(stderr,"Flushing %d recs from %s\n",number_of_records_to_skip,head->header.name); */
  482.  
  483.       while (ar_last - ar_record <= number_of_records_to_skip)
  484.         {
  485.  
  486.           /* fprintf(stderr,"Block: %d <= %d  ",ar_last-ar_record,number_of_records_to_skip); */
  487.           number_of_records_to_skip -= (ar_last - ar_record);
  488.           flush_archive ();
  489.           number_of_blocks_read++;
  490.           /* fprintf(stderr,"Block %d left\n",number_of_records_to_skip); */
  491.         }
  492.       ar_record += number_of_records_to_skip;
  493.       /* fprintf(stderr,"Final %d\n",number_of_records_to_skip); */
  494.       number_of_records_to_skip = 0;
  495.       continue;
  496.     }
  497.  
  498.       /* copy_header: */
  499.       new_block[number_of_new_records] = *head;
  500.       number_of_new_records++;
  501.       number_of_records_needed--;
  502.       number_of_records_to_keep = (hstat.st_size + RECORDSIZE - 1) / RECORDSIZE;
  503.       userec (head);
  504.       if (number_of_records_needed == 0)
  505.     write_block (1);
  506.       /* copy_data: */
  507.       number_of_kept_records_in_block = ar_last - ar_record;
  508.       if (number_of_kept_records_in_block > number_of_records_to_keep)
  509.     number_of_kept_records_in_block = number_of_records_to_keep;
  510.  
  511. #ifdef JUNK_DEBUG
  512.       fprintf(stderr,"Need %d kept_in %d keep %d\n",blocking,number_of_kept_records_in_block,number_of_records_to_keep);
  513. #endif
  514.  
  515.       while (number_of_records_to_keep)
  516.     {
  517.       int n;
  518.  
  519.       if (ar_record == ar_last)
  520.         {
  521. #ifdef JUNK_DEBUG
  522.                 fprintf(stderr,"Flush. . .\n");
  523. #endif
  524.           fl_read ();
  525.           number_of_blocks_read++;
  526.           ar_record = ar_block;
  527.           number_of_kept_records_in_block = blocking;
  528.           if (number_of_kept_records_in_block > number_of_records_to_keep)
  529.         number_of_kept_records_in_block = number_of_records_to_keep;
  530.         }
  531.       n = number_of_kept_records_in_block;
  532.       if (n > number_of_records_needed)
  533.         n = number_of_records_needed;
  534.  
  535. #ifdef JUNK_DEBUG
  536.       fprintf(stderr,"Copying %d\n",n);
  537. #endif
  538.       bcopy ((void *) ar_record, (void *) (new_block + number_of_new_records), n * RECORDSIZE);
  539.       number_of_new_records += n;
  540.       number_of_records_needed -= n;
  541.       ar_record += n;
  542.       number_of_records_to_keep -= n;
  543.       number_of_kept_records_in_block -= n;
  544. #ifdef JUNK_DEBUG
  545.       fprintf(stderr,"Now new %d  need %d  keep %d  keep_in %d rec %d/%d\n",
  546.  number_of_new_records,number_of_records_needed,number_of_records_to_keep,
  547.  number_of_kept_records_in_block,ar_record-ar_block,ar_last-ar_block);
  548. #endif
  549.  
  550.       if (number_of_records_needed == 0)
  551.         {
  552.           write_block (1);
  553.         }
  554.     }
  555.     }
  556.  
  557.   write_eot ();
  558.   close_archive ();
  559.   names_notfound ();
  560. }
  561.  
  562. void
  563. write_block (f)
  564.      int f;
  565. {
  566. #ifdef JUNK_DEBUG
  567.     fprintf(stderr,"Write block %d, num_read %d\n", f, number_of_blocks_read);
  568. #endif
  569.   /* We've filled out a block.  Write it out. */
  570.  
  571.   /* Backspace back to where we started. . . */
  572.   if (archive != STDIN)
  573.     (void) move_arch (-(number_of_blocks_read + 1));
  574.  
  575.   save_block = ar_block;
  576.   ar_block = new_block;
  577.  
  578.   if (archive == STDIN)
  579.     archive = STDOUT;
  580.   fl_write ();
  581.  
  582.   if (archive == STDOUT)
  583.     archive = STDIN;
  584.   ar_block = save_block;
  585.  
  586.   if (f)
  587.     {
  588.       /* Move the tape head back to where we were */
  589.       if (archive != STDIN)
  590.     (void) move_arch (number_of_blocks_read);
  591.       number_of_blocks_read--;
  592.     }
  593.  
  594.   number_of_records_needed = blocking;
  595.   number_of_new_records = 0;
  596. }
  597.  
  598. /* Move archive descriptor by n blocks worth.  If n is positive we move
  599.    forward, else we move negative.   If its a tape, MTIOCTOP had better
  600.    work.  If its something else, we try to seek on it.  If we can't
  601.    seek, we lose! */
  602. int
  603. move_arch (n)
  604.      int n;
  605. {
  606.   long cur;
  607.  
  608. #ifdef MTIOCTOP
  609.   struct mtop t;
  610.   int er;
  611.  
  612.   if (n > 0)
  613.     {
  614.       t.mt_op = MTFSR;
  615.       t.mt_count = n;
  616.     }
  617.   else
  618.     {
  619.       t.mt_op = MTBSR;
  620.       t.mt_count = -n;
  621.     }
  622.   if ((er = rmtioctl (archive, MTIOCTOP, &t)) >= 0)
  623.     return 1;
  624.   if (errno == EIO && (er = rmtioctl (archive, MTIOCTOP, &t)) >= 0)
  625.     return 1;
  626. #endif
  627.  
  628.   cur = rmtlseek (archive, 0L, 1);
  629.   cur += blocksize * n;
  630.  
  631. #ifdef JUNK_DEBUG
  632.   fprintf(stderr,"Seek to %x\n",cur);
  633. #endif
  634.   
  635.   if (rmtlseek (archive, cur, 0) != cur)
  636.     {
  637.       /* Lseek failed.  Try a different method */
  638.       msg ("Couldn't re-position archive file.");
  639.       exit (EX_BADARCH);
  640.     }
  641.   return 3;
  642. }
  643.